
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.geektimes.configuration.microprofile.config.source.MapBasedConfigSource;

import javax.servlet.ServletRequest;
import java.util.Map;

import static java.lang.String.format;

/**
 * {@link ServletRequest} {@link ConfigSource}
 *
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 * @since 1.0.0
 */
public class ServletRequestConfigSource extends MapBasedConfigSource {

    private final ServletRequest servletRequest;

    public ServletRequestConfigSource(ServletRequest servletRequest) {
        super(format("Servlet[name:%s] Init Parameters", servletRequest.getServletContext().getServletContextName()), 600);
        this.servletRequest = servletRequest;
    }

    @Override
    protected void prepareConfigData(Map configData) throws Throwable {
        configData =  servletRequest.getParameterMap();
    }
}
